package com.liugui;

/**
 * @ClassName: SynMethodDemo
 * @Author: liugui
 * @Date: 2021-05-20 17:41
 **/
public class SynMethodDemo {
    public static void main(String[] args) {
        MyThread8 m7 = new MyThread8();
        Thread t1 = new Thread(m7);
        Thread t2 = new Thread(m7);
        Thread t3 = new Thread(m7);

        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread8 implements Runnable {
    private int ticket = 5;
    @Override
    public void run() {
        for (int i = 0; i < 520; i++) {
            this.sale();
        }
    }

    public synchronized void sale(){ // 声明同步方法
        if (ticket > 0){
            try {
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("卖票：ticket = " + ticket -- );
            System.out.println("买票线程：" + Thread.currentThread().getName());
        }

    }
}